package com.cjw.service.impl.soulread;

import com.cjw.enums.share.SharingType;
import com.cjw.mapper.database.SystemValueMapper;
import com.cjw.mapper.soulread.ReadMapper;
import com.cjw.mapper.soulread.ReadUserMapper;
import com.cjw.mapper.soulread.TagMapper;
import com.cjw.mapper.soulread.TagReadMapper;
import com.cjw.pojo.entity.database.SystemValue;
import com.cjw.pojo.entity.livesquare.CommentParent;
import com.cjw.pojo.entity.livesquare.LiveSharing;
import com.cjw.pojo.entity.soulread.Read;
import com.cjw.pojo.entity.soulread.ReadUser;
import com.cjw.pojo.entity.soulread.TagRead;
import com.cjw.service.livesquare.CommentService;
import com.cjw.service.soulread.HeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @create_time: 2021.12.30
 * @author : cjw
 * @descripe: 热度计算
 */
@Service("heatService")
public class HeatServiceImpl implements HeatService {
    @Autowired
    private SystemValueMapper systemValueMapper; // 获取权重

    @Autowired
    private ReadMapper readMapper; // 获取点击数,创建时间

    @Autowired
    private TagMapper tagMapper; // 获取初始值

    @Autowired
    private TagReadMapper tagReadMapper;

    @Autowired
    private ReadUserMapper readUserMapper; // 获取点赞数，收藏数

    @Autowired
    private CommentService commentService; // 评论数

    @Override
    public Double getHeat(Long readId) {
        return (getS1(readId) + getS2(readId)) * getExp(readId);
    }

    private Double getS1(Long readId) {
        TagRead tagRead = new TagRead();
        tagRead.setReadId(readId);
        List<TagRead> select = tagReadMapper.select(tagRead);
        if (select.size() == 0) {
            return 100.00;
        }
        double score = 0;
        for (TagRead tagRead1 : select) {
            Long tagId = tagRead1.getTagId();
            Double score1 = tagMapper.selectByPrimaryKey(tagId).getScore();
            if (score < score1)
                score = score1;
        }
        return 100 * score;
    }

    private Double getS2 (Long readId) {
        SystemValue systemValue = new SystemValue();
        systemValue.setName("comment");
        Double comment = systemValueMapper.selectOne(systemValue).getValue();
        systemValue.setName("love");
        Double love = systemValueMapper.selectOne(systemValue).getValue();
        systemValue.setName("collection");
        Double collection = systemValueMapper.selectOne(systemValue).getValue();
        ReadUser readUser = new ReadUser();
        readUser.setReadId(readId);
        readUser.setType(SharingType.LOVE.getCode());
        int loveCount = readUserMapper.selectCount(readUser);
        readUser.setType(SharingType.COLLECTION.getCode());
        int collectionCount = readUserMapper.selectCount(readUser);
        Integer commentNum = commentService.getCommentNum(readId, 1);
        return love * loveCount + collection * collectionCount + comment * commentNum;
    }

    private Double getExp(Long readId) {
        SystemValue systemValue = new SystemValue();
        systemValue.setName("cool");
        Double cool = systemValueMapper.selectOne(systemValue).getValue();
        Date addTime = readMapper.selectByPrimaryKey(readId).getAddTime();
        Date date = new Date();
        long hour = (date.getTime() - addTime.getTime()) / (1000 * 60 * 60);
        return Math.exp(0-(cool * hour));
    }
}
